<?php
/**
 * 总后台店铺管理
 * Created by PhpStorm.
 * User: andy
 * Date: 2019/9/15
 * Time: 下午6:34
 */

namespace app\control\controller;

use app\common\model\AdModel;
use app\common\model\AdPositionModel;
use think\facade\Request;

class AdController extends AdminBaseController
{
    /**
     * 广告列表
     */
    public function index(AdModel $ad){
        $list=$ad ->getALL();

        $this->assign("list",$list);
        return $this->fetch();
    }

    /**
     * 广告位置
     */
    public function cat(AdPositionModel $ap){
        $list=$ap->getALL();

        $this->assign("list",$list);
        return $this->fetch();
    }

    /**
     * 增加/编辑
     * @param int $id
     * @param int $pid
     * @return mixed
     */
    public function edit($id=0,AdPositionModel $ap){
        if(Request::isPost()){
            $data=Request::param();

            $status=$ap->adds($data);

            if($status['status']){
                $this->success($status['msg']);
            }else{
                $this->error($status['msg']);
            }
        }else{
            $info=$ap::getByPositionId($id);
            $this->assign("info",$info);
            $this->assign("id",$id);
            return $this->fetch();
        }
    }

    /**
     * 状态修改
     */
    public function acStatus(AdPositionModel $ap){
        $id=Request::post("id");
        $status=Request::post("status");

        $am=$ap::getByPositionId($id);
        $am->status=$status;
        $st=$am->save();

        if($st){
            $this->success("操作成功！");
        }else{
            $this->error("操作失败！");
        }
    }


    /**
     * 删除分类
     */
    public function del(AdPositionModel $ap){
        $id=Request::post("id");

        $status=$ap->del($id);

        if($status['status']){
            $this->success($status['msg']);
        }else{
            $this->error($status['msg']);
        }
    }

    /**
     * 批量删除
     */
    public function delAll(AdPositionModel $ap){
        $id=Request::post("ids");

        $error=0;
        $sussuc=0;
        foreach ($id as $vo){
            $status=$ap->del($vo);
            if(!$status['status']){
                $error++;
                continue;
            }else{
                $sussuc++;
            }
        }

        $this->success("成功删除{$sussuc}条，删除失败{$error}条");
    }

    /**
     * 增加/编辑
     * @param int $id
     * @param int $pid
     * @return mixed
     */
    public function aedit($id=0,AdModel $ap){
        if(Request::isPost()){
            $data=Request::param();

            $status=$ap->adds($data);

            if($status['status']){
                $this->success($status['msg']);
            }else{
                $this->error($status['msg']);
            }
        }else{
            $info=$ap::getByAdId($id);
            $position=AdPositionModel::where('status',1)->select();

            $this->assign("info",$info);
            $this->assign("id",$id);
            $this->assign("position",$position);
            return $this->fetch();
        }
    }

    /**
     * 状态修改
     */
    public function aacStatus(){
        $id=Request::post("id");
        $status=Request::post("status");

        $am=AdModel::getByAdId($id);
        $am->status=$status;
        $st=$am->save();

        if($st){
            $this->success("操作成功！");
        }else{
            $this->error("操作失败！");
        }
    }


    /**
     * 删除分类
     */
    public function adel(AdModel $ad){
        $id=Request::post("id");

        $status=$ad->del($id);

        if($status['status']){
            $this->success($status['msg']);
        }else{
            $this->error($status['msg']);
        }
    }

    /**
     * 批量删除
     */
    public function adelAll(AdModel $ad){
        $id=Request::post("ids");

        $error=0;
        $sussuc=0;
        foreach ($id as $vo){
            $status=$ad->del($vo);
            if(!$status['status']){
                $error++;
                continue;
            }else{
                $sussuc++;
            }
        }

        $this->success("成功删除{$sussuc}条，删除失败{$error}条");
    }

    /**
     * 排序
     */
    public function acOrder(AdModel $ad){
        $id=Request::post("id");
        $orders=Request::post("orders");

        $ad=$ad::get($id);
        $ad->list_order=$orders;
        $ad=$ad->save();

        if($ad){
            $this->success("操作成功！");
        }else{
            $this->error("操作失败！");
        }
    }
}